import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
import json

class Testdaemonset:

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/1_get_daemonset_list.yaml'))
    def test_get_daemonset_list(self, daemonsetinfo, get_env_info):
        # 获取daemonset列表接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default')
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/2_create_daemonset.yaml'))
    def test_create_daemonset(self, daemonsetinfo, get_env_info):
        # 创建daemonset接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default')
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url,json=data)
        if '不正确' not in daemonsetinfo['name']:
            daemonsetname=req.json()['metadata']['name']
            globals()['daemonsetname']=daemonsetname
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/3_get_specific_daemonset.yaml'))
    def test_get_specific_daemonset(self, daemonsetinfo, get_env_info):
        # 获取指定daemonset接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default').replace('{daemonsetname}',globals()['daemonsetname'])
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/4_update_daemonset.yaml'))
    def test_update_daemonset(self, daemonsetinfo, get_env_info):
        # 更新指定daemonset接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default').replace('{daemonsetname}',globals()['daemonsetname'])
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/5_update_section_daemonset.yaml'))
    def test_update_section_daemonset(self, daemonsetinfo, get_env_info):
        # 部分更新指定daemonset的接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default').replace('{daemonsetname}',globals()['daemonsetname'])
        method = daemonsetinfo['request']['method']
        data = daemonsetinfo['request']['data']
        header = daemonsetinfo['request']['headers']
        req = RequestUtil().send_request(method, url, headers=header,json=data)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']

    @pytest.mark.parametrize('daemonsetinfo', YamlUtil().read_testcase_yaml('v24.06/resourcemanage/daemonset/6_delete_specific_daemonset.yaml'))
    def test_delete_specific_daemonset(self, daemonsetinfo, get_env_info):
        # 删除daemonset接口
        baseUrl = get_env_info
        url = baseUrl + daemonsetinfo['request']['url'].replace('{namespace}','default').replace('{daemonsetname}',globals()['daemonsetname'])
        method = daemonsetinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == daemonsetinfo['validate'][0]['equals']['status_code']